package com.yami.shop.mp.common.platform.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.config.WxConfig;
import com.yami.shop.mp.common.model.WeiXinMenu;
import com.yami.shop.mp.common.service.WeixinMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpMenuService;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;

/**
 * @author lgh on 2019/05/13.
 */
@Slf4j
@RestController
@RequestMapping("/weixin/weixinMenu")
@Tag(name = "平台端微信菜单接口")
public class WeixinMenuController {

    @Autowired
    private WeixinMenuService weixinMenuService;
    @Autowired
    private WxConfig wxConfig;

    @GetMapping("/page")
    @Operation(summary =  "分页获取微信菜单")
    public ServerResponseEntity<IPage<WeiXinMenu>> page(@ParameterObject WeiXinMenu weiXinMenu, PageParam<WeiXinMenu> page) {
        IPage<WeiXinMenu> list = weixinMenuService.page(page);
        return ServerResponseEntity.success(list);
    }

    @GetMapping("/info")
    @Operation(summary =  "获取微信菜单信息")
    public ServerResponseEntity<String> info() {
        WeiXinMenu weiXinMenu = weixinMenuService.getById(1);
        //如果为空则创建一个
        if (weiXinMenu == null) {
            weiXinMenu = new WeiXinMenu();
            weiXinMenu.setId(1);
            weiXinMenu.setMenu("{ \"button\": [] }");
            weiXinMenu.setUpdateTime(new Date());
            weixinMenuService.save(weiXinMenu);
        }
        return ServerResponseEntity.success(weiXinMenu.getMenu());
    }

    @PostMapping
    @Operation(summary =  "保存微信菜单")
    public ServerResponseEntity<Void> save(@RequestBody @Valid WeiXinMenu weiXinMenu) {
        weixinMenuService.save(weiXinMenu);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary =  "修改微信菜单")
    public ServerResponseEntity<Void> update(@RequestBody @Valid WeiXinMenu weiXinMenu) {
        System.err.println(weiXinMenu.getMenu());
        weiXinMenu.setId(1);
        weiXinMenu.setUpdateTime(new Date());
        weiXinMenu.setMenu(weiXinMenu.getMenu());
        weixinMenuService.updateById(weiXinMenu);
        return ServerResponseEntity.success();
    }

    @PutMapping("release")
    @Operation(summary =  "发布微信菜单")
    public ServerResponseEntity<Void> release() {
        try {
            //获取
            WeiXinMenu weiXinMenu = weixinMenuService.getById(1);
            //对接微信接口发布菜单
            WxMpMenuService menuService = wxConfig.getWxMpService().getMenuService();
            menuService.menuDelete();
            menuService.menuCreate(weiXinMenu.getMenu());
        } catch (WxErrorException e) {
            log.error("发布微信菜单错误:", e);
            throw new YamiShopBindException("error: Code=" + e.getError().getErrorCode() + ", Msg=" + e.getError().getErrorMsg());
        }
        return ServerResponseEntity.success();
    }

    @DeleteMapping("/{id}")
    @Operation(summary =  "通过id删除微信菜单")
    @Parameter(name = "id", description = "微信菜单id", required = true)
    public ServerResponseEntity<Void> delete(@PathVariable Long id) {
        weixinMenuService.removeById(id);
        return ServerResponseEntity.success();
    }
}
